package visualizer;

import visualizer.drawable.Drawable;
import visualizer.drawable.DrawableCollection;
import visualizer.graphics.HsbColor;
import visualizer.input.Component;
import visualizer.input.InputListener;

import java.util.LinkedList;
import java.util.List;

import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PVector;

/**
 * A core applet window class that delegates setup, input handling and drawing
 * to external instances.
 * 
 * Note: This class should not have to be modified in ordinary circumstances.
 * Implement any new features as combinations of Drawable/InputListener classes
 * and set them up in the Bootstrap.
 */
public class App extends PApplet {
	private DrawableCollection drawables = new DrawableCollection();
	private List<InputListener> listeners = new LinkedList<InputListener>();
	private HsbColor background = new HsbColor(193, 39, 65);


	public App add(Component<?> c)
	{
		addDrawable(c);
		addListener(c);
		return this;
	}

	public App addDrawable(Drawable d)
	{
		drawables.add(d);
		return this;
	}

	public App addListener(InputListener l)
	{
		listeners.add(l);
		return this;
	}

	public void setup()
	{
		new Bootstrap().init(this);
		colorMode(RGB, 255);
		background(color(102, 153 ,167));
		colorMode(HSB, 255);
	}

	public void draw()
	{
		PGraphics g = createGraphics();
		g.beginDraw();
		g.smooth();

		for (Drawable d : drawables) {
			d.draw(g);
		}

		g.endDraw();
		image(g, 0, 0);
	}

	public void keyPressed()
	{
		for (InputListener l : listeners) {
			l.keyPressed(key);
		}
	}

	public void mousePressed()
	{
		for (InputListener l : listeners) {
			l.mousePressed(new PVector(mouseX, mouseY));
		}
	}

	public void mouseMoved() {
		for (InputListener l : listeners) {
			l.mouseMoved(new PVector(mouseX, mouseY));
		}
	}

	public PGraphics createGraphics()
	{
		PGraphics g = createGraphics(width, height, P2D);
		g.smooth();
		colorMode(RGB, 255);
		background(color(102, 153 ,167));
		g.colorMode(HSB, 255);
		// Huom! v��r� taustav�ri
		return g;
	}
}
